# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Aug. 12, 2023
# * Supported by: National Key Research and Development Program of China
# */

from phot_server.core.exception import ParseError
from phot_server.rpc.device_data_type.ttypes import PhotGrid
from phot_server.utils.toml_parser import parse_toml, save_toml
from phot_server.utils.type_convert import dict_to_PhotGrid
from phot_server.utils.type_convert import PhotGrid_to_dict


def parse_phtd(path: str) -> PhotGrid:
    grid_dict = parse_toml(path)
    if grid_dict.get("Grid", None) is None:
        raise ParseError("Photlab Grid File format error. ", path)

    grid = dict_to_PhotGrid(grid_dict["Grid"])
    return grid


def save_phtd(path: str, grid: PhotGrid):
    save_toml(path, PhotGrid_to_dict(grid))
